<template>
  <div class="my-orders">
    <h1>我的订单</h1>
    <div v-if="myOrders.length > 0">
      <div v-for="order in myOrders" :key="order.id" class="order-card">
        <div class="order-header">
          <div>订单编号：{{ order.id }}</div>
          <div>创建时间：{{ order.time }}</div>
          <div>状态：{{ order.status }}</div>
        </div>
        <div class="order-details">
          <h3>订单详情</h3>
          <div v-for="item in order.goods" :key="item.id" class="order-item">
            <div class="item-name">{{ item.name }} x{{ item.num }}</div>
            <div class="item-price">单价：￥{{ item.price }}</div>
            <div class="item-subtotal">小计：￥{{ item.price * item.num }}</div>
          </div>
          <div class="total">总计：￥{{ order.total }}</div>
        </div>
        <div class="order-address">
          <h3>收货地址</h3>
          <div>姓名：{{ order.address.name }}</div>
          <div>电话：{{ order.address.phone }}</div>
          <div>地址：{{ order.address.address }}</div>
        </div>
      </div>
    </div>
    <div v-else class="no-orders">
      暂无订单
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const myOrders = ref([]);

onMounted(() => {
  // 从 localStorage 中读取我的订单列表
  const storedMyOrders = localStorage.getItem('myOrders');
  if (storedMyOrders) {
    myOrders.value = JSON.parse(storedMyOrders);
  }
});
</script>

<style scoped>
.my-orders {
  padding: 1rem;
  max-width: 800px;
  margin: 0 auto;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  color: #333;
  margin-bottom: 1rem;
}

.order-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  margin-bottom: 1rem;
  padding: 1rem;
}

.order-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  color: #333;
}

.order-details, .order-address {
  margin-bottom: 1rem;
  border-bottom: 1px solid #e0e0e0;
  padding-bottom: 1rem;
}

.order-details h3, .order-address h3 {
  margin-bottom: 0.5rem;
  color: #333;
}

.order-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  border-bottom: 1px solid #e0e0e0;
  padding-bottom: 1rem;
}

.item-name, .item-price, .item-subtotal {
  color: #555;
}

.total {
  font-weight: bold;
  margin-top: 1rem;
  text-align: right;
  color: #333;
  font-size: 1.2rem;
}

.no-orders {
  text-align: center;
  color: #555;
  margin-top: 2rem;
}
</style>